<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>改变this指向-call</title>
</head>

<body>
  <script>
    // ============================== 改变this指向 - call ============================== 
    const obj = { name: '佩奇' }

    // call() 作用： 第一个调用函数  第二改变this指向
    function fun(x, y) {
      console.log(this)
      console.log(x + y)
      console.log(arguments)
    }
    fun(1, 2)
    fun.call(obj, 10, 12, 22, 2132, 123)
    // 2. call的应用场景 - 检测数据类型
    // 2.1 typeof 检测数据类型不够精确的
    // 2.2 Object.prototype.toString()  返回的结果是[object xxx类型]
    console.log(Object.prototype)
    console.log(Object.prototype.toString())
    console.log(Object.prototype.toString.call(3))
    console.log(Object.prototype.toString.call('4'))
    console.log(Object.prototype.toString.call(null))
    console.log(Object.prototype.toString.call(undefined))
    console.log(Object.prototype.toString.call([]))
    console.log(Object.prototype.toString.call({}))
    console.log(Object.prototype.toString.call(new Date))

    function getType(data) {
      return Object.prototype.toString.call(data)
    }
  </script>
</body>

</html>